package com.kkb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.common.utils.StringUtils;
import com.kkb.domain.AjaxResult;
import com.kkb.page.PageDomain;
import com.kkb.page.TableDataInfo;
import com.kkb.page.TableSupport;
import com.kkb.pojo.TbBusiness;

import com.kkb.service.TbBusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

import static com.kkb.domain.AjaxResult.error;

@Controller
@RequestMapping("/Commoditylist")
public class CommodityController {
    @Autowired
    private TbBusinessService tbBusinessService;

    //商户列表
    @GetMapping("/getCommodity")
    public String getCommodity() {
        return "Commodity/Commoditylist";
    }

    //商户分类列表
    @ResponseBody
    @RequestMapping("/getCommoditylist")
    public TableDataInfo getCommoditylist() {
        startPage();
        List<TbBusiness> tabusiness = tbBusinessService.getCommodity();
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(tabusiness);
        rspData.setTotal(new PageInfo(tabusiness).getTotal());
        return rspData;
    }

    /**
     * 设置请求分页数据
     */
    private void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            PageHelper.startPage(pageNum, pageSize);
        }
    }

    @GetMapping("/add")
    public String add() {
        return "Commodity/add";
    }


    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(@Validated TbBusiness tbBusiness) {
//        template.setCategoryId(1);
//        template.setIsDelete("0");

        tbBusiness.setCreateid(1);
        tbBusiness.setUpdateUser(1);
        Date date = new Date();
        tbBusiness.setCreatetime(date);
        return tbBusinessService.insertTbBusiness(tbBusiness) > 0 ? AjaxResult.success() : error();

    }

    @GetMapping("/delete/{businessId}")
    @ResponseBody
    public AjaxResult delete(@PathVariable("businessId") Integer businessId) {

        return tbBusinessService.deleteCommodityById(businessId) > 0 ? AjaxResult.success() : error();

    }


    @GetMapping("/edit/{businessId}")
    public String edit(@PathVariable("businessId") Integer businessId, ModelMap map) {

        TbBusiness tbBusiness = tbBusinessService.SelectCommodityById(businessId);
        map.put("tbBusiness", tbBusiness);
        return "Commodity/edit";

    }

    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(@Validated TbBusiness tbBusiness) {
        tbBusiness.setCreateid(1);
        tbBusiness.setUpdateUser(1);
        return tbBusinessService.updateCommodity(tbBusiness) > 0 ? AjaxResult.success() : error();
    }

}
